#!/bin/sh
###################
source /etc/profile
source /root/.bash_profile
##################
cd `dirname $0`
#自诊报告课程/专业/教师/学生层面数据项定时脚本 学年

#所执行的脚本课程层面
REPORT_COURSE=qu_course_diagnosis_report.sh
#所执行的脚本课程层面（里面的方法）
SHELL_COURSE="
Course_type
School_enterprise_cooperation
Excellent_course
Number_of_courses_offered
Number_of_classes_in_the_course
Number_of_course_learners
Average_course_score
Number_of_registered_students
Number_of_person_in_charge
"

#所执行的脚本专业层面
REPORT_MAJOR=qu_major_diagnosis_report.sh
#所执行的脚本专业层面
SHELL_MAJOR="
qu_major_diagnosis_report_SSXBMC
import_table_SFXDXTZZY
import_table_SZSJ
import_table_GZYBJSL
import_table_ZYDTRXM
import_table_XYZRJSRS
import_table_BSYJSXLJSRS
import_table_SSXLJSRS
import_table_BZYGJZCJSRS
import_table_BZYSSSZJSRS
import_table_BZY
import_table_DLFBLWSL
import_table_LWZZBHBXJSDLWSL
import_table_HJXMSL
import_table_GJJHJXMSL
import_table_SJHJXMSL
import_table_SSJHJXMSL
import_table_XJHJXMSL
Professional_leader_no
"
#所执行的脚本学生层面
REPORT_STUDENT=qu_student_diagnosis_report.sh
#所执行的脚本学生层面
SHELL_STUDENT="
import_table_JYTSSL
"
#所执行的脚本教师层面
REPORT_TEACHER=qu_teacher_diagnosis.sh
#所执行的脚本教师层面（里面的方法）
SHELL_TEACHER="
teacher_native_place
teacher_ethnic
teacher_education
teacher_subordinate_departments
teacher_professional_title
teacher_Number_of_substitute_classes
teacher_Publishing_papers
teacher_Participating_works
teacher_Number_of_patents
teacher_Social_services
teacher_Training_times
teacher_Educational_background_promotion
"


#日志路径
function getRunLogPath(){
    datetime=$(date --date "0 days ago" +%Y%m%d)
    if [ ! -d "./logs" ];then
        mkdir ./logs
    fi
    LOG_RUN_PATH=/root/etl/SXNYZY/Timing/selfdiagnosisreport/logs/$0_${datetime}.log
}


function doconmand(){
    start=$(date +%s)
    #读取文件的每一行
	for comand in ${comands}
	do
		ps -ef|grep $comand|grep -v grep|cut -c 9-15|xargs kill -9
        sh $REPORT "${comand}"
        end=$(date +%s)
		 if [ $? -eq 0 ]; then
	    	echo " $comand succeed>>>  耗时$(( $end - $start ))s " >>  ${LOG_RUN_PATH} 2>&1
		 else
		    echo " $comand failed>>>  耗时$(( $end - $start ))s "  >>  ${LOG_RUN_PATH} 2>&1;
		    echo " ## sh $comand 脚本执行错误，退出命令  exit 1  ##"  >>  ${LOG_RUN_PATH} 2>&1;
		    exit 1;
		 fi

	done
	end=$(date +%s)
	echo " 耗时$(( $end - $start ))s" >>  ${LOG_RUN_PATH} 2>&1
	#删除脚本执行过程中产生的Java文件
	rm -rf *.java
}
#课程层面
function docourse() {
   echo "-------课程层面开始全部执行--------" >>  ${LOG_RUN_PATH} 2>&1
   comands=${SHELL_COURSE[*]}
   REPORT=${REPORT_COURSE}
   doconmand
}

function domajor() {
   echo "-------专业层面开始全部执行--------" >>  ${LOG_RUN_PATH} 2>&1
   comands=${SHELL_MAJOR[*]}
   REPORT=${REPORT_MAJOR}
   doconmand
}

function dostudent() {
   echo "-------学生层面开始全部执行--------" >>  ${LOG_RUN_PATH} 2>&1
   comands=${SHELL_STUDENT[*]}
   REPORT=${REPORT_STUDENT}
   doconmand
}

function doteacher() {
   echo "-------教师层面开始全部执行--------" >>  ${LOG_RUN_PATH} 2>&1
   comands=${SHELL_TEACHER[*]}
   REPORT=${REPORT_TEACHER}
   doconmand
}

getRunLogPath
echo "日志路径:"$LOG_RUN_PATH
currentPath=/root/etl/SXNYZY/SelfDiagnosisReport
echo "脚本路径:"$currentPath
comands=()

if [ $? -eq 0 ]; then
	cd  $currentPath/course
	docourse
fi

if [ $? -eq 0 ]; then
	cd  $currentPath/major
	domajor
fi

if [ $? -eq 0 ]; then
	cd  $currentPath/student
	dostudent
fi

if [ $? -eq 0 ]; then
	cd  $currentPath/teacher
	doteacher
fi
echo "*********所有脚本执行完毕*********" >>  ${LOG_RUN_PATH} 2>&1

